package jp.co.piratealliance.pirateAllianceWeb.web.action.order;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import jp.co.piratealliance.pirateAllianceWeb.common.base.BaseAction;
import jp.co.piratealliance.pirateAllianceWeb.def.Constants;
import jp.co.piratealliance.pirateAllianceWeb.dto.order.EventInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.order.ScheduleInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.helper.CommonHelper;
import jp.co.piratealliance.pirateAllianceWeb.service.order.OrderService;


/**
 * copyright pirate alliance team 2014
 *
 * @author lunxing
 */

public class EventAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private OrderService orderService;

	private List<EventInfoDto> eventInfoDtoList;
	
	private EventInfoDto eventInfoDto;
	
	private String schedule_id;
	
	private String order_id;

	public String insertEvent()
	{
		List<ScheduleInfoDto> scheduleInfoDtoList = new ArrayList<ScheduleInfoDto>();
		if (this.eventInfoDtoList.size() > 0) {
			this.orderService.deleteEventByOrderId(this.eventInfoDtoList.get(0).getOrder_id());
			scheduleInfoDtoList = this.orderService.getScheduleByOrderId(this.eventInfoDtoList.get(0).getOrder_id());
		}
		
		List<EventInfoDto> list = new ArrayList<EventInfoDto>();
		for (int i = 0; i < scheduleInfoDtoList.size(); i++) {
			String scheduleIdDB = scheduleInfoDtoList.get(i).getSchedule_id();
			for (int j = 0; j < this.eventInfoDtoList.size(); j++) {
				EventInfoDto eventInfoDto = eventInfoDtoList.get(j);
				String scheduleIdClient = eventInfoDto.getSchedule_id();
				if (true == scheduleIdDB.equals(scheduleIdClient)) {
					String event_id = eventInfoDto.getEvent_id();
					if (null == event_id || "".equals(event_id)) {
						event_id = CommonHelper.getSequence(Constants.TB_EVENT);
						eventInfoDto.setEvent_id(event_id);
					}
					eventInfoDto.setEvent_identifier("");
					
					int count = this.orderService.insertEvent(eventInfoDto);

					if (1 == count) {
						list.add(eventInfoDto);
					}
				}
			}
		}
		
		this.eventInfoDtoList.clear();
		this.eventInfoDtoList.addAll(list);
		
		return SUCCESS;
	}

	public String updateEvent()
	{	
		List<String> scheduleIds = new ArrayList<String>();
		for (int i = 0; i < this.eventInfoDtoList.size(); i++) {
			scheduleIds.add(this.eventInfoDtoList.get(i).getSchedule_id());
		}
		
		HashSet<Object> hashSet = new HashSet<Object>(scheduleIds);
		List<String> scheduleIdsNoRep = new ArrayList(hashSet);
		
		List<ScheduleInfoDto> scheduleInfoDtoList = this.orderService.getScheduleByOrderId(this.order_id);
		
		Boolean flag = false;
		for (int i = 0; i < scheduleIdsNoRep.size(); i++) {
			flag = false;
			String scheduleIdClient = scheduleIdsNoRep.get(i);
			for (int j = 0; j < scheduleInfoDtoList.size(); j++) {
				String scheduleIdDB = scheduleInfoDtoList.get(j).getSchedule_id();
				if (scheduleIdClient == scheduleIdDB) {
					flag = true;
					break;
				}
			}
			
			if (false == flag) {
				this.orderService.deleteEventByScheduleId(scheduleIdClient);
			}
		}
		
		this.eventInfoDtoList = this.orderService.getEventByOrderId(this.order_id);
		return SUCCESS;
	}
	
	public String getEventByScheduleId()
	{
		this.eventInfoDtoList = this.orderService.getEventByScheduleId(this.schedule_id);
		return SUCCESS;
	}
	
	public String getEventByOrderId()
	{
		this.eventInfoDtoList = this.orderService.getEventByOrderId(this.order_id);
		return SUCCESS;
	}

	/**
	 * @return the orderService
	 */
	public OrderService getOrderService() {
		return orderService;
	}

	/**
	 * @param orderService the orderService to set
	 */
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	/**
	 * @return the eventInfoDtoList
	 */
	public List<EventInfoDto> getEventInfoDtoList() {
		return eventInfoDtoList;
	}

	/**
	 * @param eventInfoDtoList the eventInfoDtoList to set
	 */
	public void setEventInfoDtoList(List<EventInfoDto> eventInfoDtoList) {
		this.eventInfoDtoList = eventInfoDtoList;
	}

	/**
	 * @return the eventInfoDto
	 */
	public EventInfoDto getEventInfoDto() {
		return eventInfoDto;
	}

	/**
	 * @param eventInfoDto the eventInfoDto to set
	 */
	public void setEventInfoDto(EventInfoDto eventInfoDto) {
		this.eventInfoDto = eventInfoDto;
	}

	/**
	 * @return the schedule_id
	 */
	public String getSchedule_id() {
		return schedule_id;
	}

	/**
	 * @param schedule_id the schedule_id to set
	 */
	public void setSchedule_id(String schedule_id) {
		this.schedule_id = schedule_id;
	}

	/**
	 * @return the order_id
	 */
	public String getOrder_id() {
		return order_id;
	}

	/**
	 * @param order_id the order_id to set
	 */
	public void setOrder_id(String order_id) {
		this.order_id = order_id;
	}

}
